new Vue({
    el:"#app",
    data: {
        fullscreenLoading:false,
        screenLoading:false,
        dialogFormVisible: false,
        adImgUrl: '',
        uploadAdUrl:'',
        productId: null,
        productName:'',
        adProductH5Url: '',
        adCateId:null,
        isUsed: 1,
        pageNo: 1,
        pageSize: 15,
        total: 0,
        pages: 0,
        queryContent: {
            keyword:''
        },
        productData:[],
        cateList:[],
        tempId:'',
        tempName:'',
        id:null,
        onProcess: false,
        uploadPercent: 0
    },
    mounted:function(){
        var _this=this;
        _this.loadCateList();
        _this.getProductAdInfo();
    },
    methods:{
        trim:function(str){
            if(isNaN(str)){
                return str.replace(/(^\s*)|(\s*$)/g, "");
            }else{
                return str
            }
        },
        loadCateList:function () {
            var _this = this;
            axios.get(request_prefix + "/cash/adCate/list")
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.cateList = res.data.data;
                        _this.getProductAdInfo();
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        handleAvatarProcess: function (event, file, fileList) {
            var _this = this;
            _this.showStatus();
            _this.onProcess = true;
        },
        showStatus: function () {
            var _this = this;
            var intervalId = setInterval(function () {
                if (_this.uploadPercent < 100) {
                    _this.uploadPercent += 5;
                }
            }, 100);
        },
        handleAvatarSuccess(res, file) {
            var _this = this;
            _this.uploadAdUrl = res.data.pictureUrl;
            _this.adImgUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            var isJPG = file.type === 'image/jpeg';
            var isPNG = file.type === 'image/png';
            var isGIF = file.type === 'image/gif';
            var isLt1M = file.size / 1024 / 1024 < 1;

            if (!isJPG&&!isPNG&&!isGIF) {
                this.$message.error('上传图片格式错误!!');
            }
            if (!isLt1M) {
                this.$message.error('上传图片大小不能超过 1MB!');
            }
            if(this.adImgUrl != '') {
                this.adImgUrl = '';
            }
            this.uploadPercent = 0;
            this.onProcess = false;

            return (isJPG||isPNG||isGIF) && isLt1M;
        },
        getProductAdInfo:function () {
            var _this = this;
            var id = localStorage.getItem("ad_id_");
            if(parseInt(id) == null) {
                _this.$message.error("无法正确获取编辑信息，请联系开发");
                return;
            }
            _this.id = id;
            axios.get(request_prefix+"/cash/ad/detail",{
                params: {
                    id: id
                }
            })
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.productName = res.data.data.productName;
                        _this.productId = res.data.data.productId;
                        _this.isUsed = parseInt(res.data.data.isUsed);
                        if(res.data.data.adProductH5Url != null || res.data.data.adProductH5Url != '') {
                            _this.adProductH5Url = res.data.data.adProductH5Url;
                        }
                        _this.id = parseInt(res.data.data.id);
                        _this.uploadAdUrl = res.data.data.adImgUrl;
                        _this.adImgUrl = _this.uploadAdUrl;
                        _this.adCateId = res.data.data.cateId;
                        if(_this.adCateId == 0) {
                            _this.adCateId = null;
                        }
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求失败，无法获取数据");
                })
        },
        selectProduct:function () {
            var _this = this;
            axios.get(request_prefix+"/cash/ad/productList",{
                params: {
                    currentPage: _this.pageNo,
                    keyword: _this.queryContent.keyword
                }
            })
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.productData = res.data.data.dataList;
                        if(_this.dialogFormVisible == false) {
                            _this.dialogFormVisible = true;
                        }
                    } else {
                        _this.$message.error("未能获取到数据");
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        handleCurrentChange:function () {
            var _this = this;
            axios.get(request_prefix+"/cash/ad/productList",{
                params: {
                    currentPage: _this.pageNo,
                    keyword: _this.queryContent.keyword
                }
            })
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.productData = res.data.data.dataList;
                    } else {
                        _this.$message.error("未能获取到数据");
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        resetQuery:function () {
            var _this = this;
            _this.queryContent.keyword = '';
            _this.pageNo = 1;
            _this.selectProduct();
        },
        getItemInfo:function (id,name) {
            var _this = this;
            _this.tempId = id;
            _this.tempName = name;
        },
        confirmGetInfo:function () {
            var _this = this;
            if(_this.tempId === '' || _this.tempName === '') {
                _this.$message.warning("请选择产品");
                return;
            }
            _this.productId = parseInt(_this.tempId);
            _this.productName = _this.tempName;
            _this.dialogFormVisible = false;
            _this.tempId = '';
            _this.tempName = '';
        },
        clearProduct:function () {
            var _this = this;
            _this.productName = '';
            _this.productId = null;
        },
        updateAdInfo:function () {
            var _this = this;
            if(_this.productId === null || _this.uploadAdUrl === '' || _this.adProductH5Url === '' || _this.isUsed == null) {
                _this.$message.warning("信息不完整，无法保存");
                return;
            }
            var isValidUrl = _this.validateURL(_this.adProductH5Url);
            if(!isValidUrl) {
                _this.$message.warning("广告链接不符合要求");
                return;
            }
            var data = new URLSearchParams();
            data.append("productId",parseInt(_this.productId));
            data.append("adImgUrl",_this.uploadAdUrl);
            data.append("id",parseInt(_this.id));
            data.append("adProductH5Url",_this.adProductH5Url);
            data.append("isUsed",_this.isUsed);
            data.append("cateId",_this.adCateId);
            axios.post(request_prefix+"/cash/ad/update",data)
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.$message.success("更新成功，2秒后跳转到列表页");
                        setTimeout(function () {
                            window.location.href = request_prefix+"/cash/ad/listPage";
                        },2000)
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        toBackList:function () {
            window.location.href = request_prefix+"/cash/ad/listPage";
        },
        validateURL:function(textval) {
            const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
            return urlregex.test(textval)
        }
    }
});